//
//  JDBaseVCAblumExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/11/10.
//

import UIKit
import TZImagePickerController
//import HXPhotoPicker
import HXPhotoPicker

class JDBaseVCAblumExtension: NSObject {

}

enum AblumType {
    /// 相册去图片
    case photoAblum
    /// 相机拍照
    case photoCamera
    /// 相册取视频
    case videoAblum
    /// 相机录像
    case videoCamera
}


extension JDBaseViewController {
    
    func openAlbumAlert(_ selectedCompleted: @escaping ((AblumType,[UIImage])->())) {
        
        let alertVC = UIAlertController(title: "请选择上传方式", message: "", preferredStyle: .actionSheet)
        
        let albumAction = UIAlertAction(title: "相册", style: .default) {[weak self] _ in
            self?.openAlbum { images in
                if let imgs = images, imgs.count > 0 {
                    selectedCompleted(.photoAblum, imgs)
                }
            }
        }
        
        let cameraAction = UIAlertAction(title: "相机", style: .default) {[weak self] _ in
            self?.caremaPhotoCompleted = selectedCompleted
            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = .camera
            self?.present(imagePickerController, animated: true)
//            self?.openCarema()
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel) { _ in
        }
        alertVC.addAction(albumAction)
        alertVC.addAction(cameraAction)
        alertVC.addAction(cancelAction)
        alertVC.modalPresentationStyle = .fullScreen
        self.present(alertVC, animated: true)
    }
    
    /// 选择视频
    func openVideoAlert(_ selectedCompleted: @escaping ((AblumType,UIImage,String)->())) {
        
        let alertVC = UIAlertController(title: "请选择上传方式", message: "", preferredStyle: .actionSheet)
        
        let albumAction = UIAlertAction(title: "录像", style: .default) {[weak self] _ in
            self?.startVideo()
        }
        
        let cameraAction = UIAlertAction(title: "本地视频", style: .default) {[weak self] _ in
            self?.openVideo(selectedCompleted: { videoImage, videoUrlPath in
                if let image = videoImage, let utlPath = videoUrlPath {
                    selectedCompleted(.videoAblum, image, utlPath)
                }
            })
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel) { _ in
            
        }
        alertVC.addAction(albumAction)
        alertVC.addAction(cameraAction)
        alertVC.addAction(cancelAction)
        alertVC.modalPresentationStyle = .fullScreen
        self.present(alertVC, animated: true)
    }
    
    func openCarema() {
        // https://github.com/SilenceLove/HXPhotoPicker
        let config = CameraConfiguration()
        
        let camerController = CameraController(config: config, type: .photo)
//        camerController.cameraDelegate = self
        self.presentVC(camerController)

    }
    
    // deepseek给的代码
    func openCameraWithCrop() {
//        let config = HXPhotoConfiguration()
//        
//        // 启用相机
//        config.openCamera = true
//        config.cameraType = .custom
//        
//        // 设置裁剪选项
//        config.clarityScale = 2.0  // 清晰度
//        config.photoCanEdit = true // 允许编辑照片
//        config.videoCanEdit = false // 禁用视频编辑
//        
//        // 裁剪配置
//        config.photoEditConfigur.aspectRatio = .ratio_1x1 // 裁剪比例 1:1
//        config.photoEditConfigur.onlyCliping = true // 只裁剪
//        config.photoEditConfigur.isRoundCliping = false // 是否圆形裁剪
//        
//        // 其他配置
//        config.singleSelectedMode = true // 单选模式
//        config.singleJumpEdit = true // 直接跳转到编辑界面
//        
//        let photoPicker = HXPhotoPickerController(with: config)
//        photoPicker.modalPresentationStyle = .fullScreen
//        photoPicker.delegate = self
//        present(photoPicker, animated: true, completion: nil)
    }
    
    /// 打开图片相册
    /// - Parameters:
    ///   - maxImagesCount: 最多可以选择几张照片
    ///   - columnNumber: 每行展示几张照片
    ///   - delegate: 代理
    func openAlbum(maxImagesCount: Int = 1 , columnNumber: Int = 4, selectedCompleted: @escaping (([UIImage]?)->())) {
        if let imagePickerVC = TZImagePickerController(maxImagesCount: maxImagesCount, columnNumber: columnNumber, delegate: self, pushPhotoPickerVc: true) {
            // 是否允许选择原图
            imagePickerVC.isSelectOriginalPhoto = true
            // 在内部显示拍照按钮
            imagePickerVC.allowTakePicture = false
            imagePickerVC.allowTakeVideo = false
            imagePickerVC.allowPickingImage = true
            imagePickerVC.allowPickingVideo = false
        
            imagePickerVC.preferredLanguage = "zh-Hans"
//            imagePickerVC.cancelBtnTitleStr = "取消"
//            imagePickerVC.doneBtnTitleStr = "确定"
//            /// 首选语言，如果设置了就用该语言，不设则取当前系统语言。
//            /// 支持zh-Hans、zh-Hant、en、vi等值，详见TZImagePickerController.bundle内的语言资源
//            @property (copy, nonatomic) NSString *preferredLanguage;
            
            
            imagePickerVC.showSelectBtn = false
            /// 是否允许裁剪
            imagePickerVC.allowCrop = true
            imagePickerVC.cropRect = CGRectMake(0, screenHeight/2.0-screenWidth/2.0, screenWidth, screenWidth)
            
            // 已选数字的背景颜色
            imagePickerVC.iconThemeColor = .red
            imagePickerVC.showPhotoCannotSelectLayer = true
            // 不可以选择的颜色
            imagePickerVC.cannotSelectLayerColor = .white.withAlphaComponent(0.8)
            
            imagePickerVC.statusBarStyle = .lightContent
            imagePickerVC.didFinishPickingPhotosWithInfosHandle = { images, assets, isSelectOriginalPhoto, infos in
                print("选择了个数:\(String(describing: images?.count))")
                selectedCompleted(images)
            }
            self.presentVC(imagePickerVC)
        }
        
    }
    
//    var selectedVideoCompleted: ((UIImage?,String?)->())?
    
    func openVideo(maxImagesCount: Int = 1 , columnNumber: Int = 4, selectedCompleted: @escaping ((UIImage?,String?)->())) {
        if let videoPickerVC = TZImagePickerController(maxImagesCount: maxImagesCount, columnNumber: columnNumber, delegate: self, pushPhotoPickerVc: true) {
            // 是否允许选择原图
            videoPickerVC.isSelectOriginalPhoto = true
            // 在内部显示拍照按钮
            videoPickerVC.allowTakePicture = false
            videoPickerVC.allowTakeVideo = false
            videoPickerVC.allowPickingImage = false
            videoPickerVC.allowPickingVideo = true
            // 已选数字的背景颜色
            videoPickerVC.iconThemeColor = .red
            videoPickerVC.showPhotoCannotSelectLayer = true
            // 不可以选择的颜色
            videoPickerVC.cannotSelectLayerColor = .white.withAlphaComponent(0.8)
            
            videoPickerVC.statusBarStyle = .lightContent
            
            self.selectedVideoCompleted = selectedCompleted
            
            self.presentVC(videoPickerVC)
        }
        
    }
    
}

extension JDBaseViewController: TZImagePickerControllerDelegate {
    
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingVideo coverImage: UIImage!, sourceAssets asset: PHAsset!) {
        
        TZImageManager().getVideoOutputPath(with: asset, presetName: AVAssetExportPresetLowQuality) {[weak self] outputPath in
//            print("视频路径吗:\(outputPath)")
            self?.selectedVideoCompleted?(coverImage,outputPath)
        } failure: { errorMsg, error in
            
        }
        
    }
}
